package com.iemes.controller.client;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.transaction.Transaction;
import org.omg.CORBA.TRANSACTION_ROLLEDBACK;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ClientPlannerFormMap;
import com.iemes.entity.OrderFormMap;
import com.iemes.entity.OrderperFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.order.OrderMapper;
import com.iemes.mapper.order.OrderpreMapper;
import com.iemes.mapper.planner.ClientPlannerMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;

@RequestMapping("/client/")
@Controller
public class ClientController extends BaseController {

	@Inject
	private OrderMapper orderMapper;
	@Inject
	private ClientPlannerMapper clientPlannerMapper;

	@RequestMapping("getall")
	public String getall(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/client/client_list";
	}

	// 订单分页
	@ResponseBody
	@RequestMapping("myorder_findByPage")
	public PageView findByPage(String pageNow, String pageSize)
			throws Exception {
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		int client_no = Integer.valueOf(userFormMap.getStr("client_id"));
		OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);
		orderFormMap.put("client_no", client_no);
		List<Map<String, Object>> orderlist = clientPlannerMapper
				.getAllName(orderFormMap);
		LuogicPaging(pageNow, pageSize, orderlist);
		return pageView;
	}

	// 新增订单页面
	@RequestMapping("add_client")
	public String addUI(Model model) throws Exception {
		String id = getPara("id");
		OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id,
				OrderFormMap.class);
		model.addAttribute("order", orderFormMap);
		return Common.BACKGROUND_PATH + "/client/client_add";
	}

	// 新增订单页面
	@RequestMapping("client_addEntity")
	@ResponseBody
	@SystemLog(module = "订单管理", methods = "订单管理-客户添加订单")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String client_addEntity() {
		OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);

		OrderFormMap order = orderMapper.findbyFrist("id",
				orderFormMap.getStr("id"), OrderFormMap.class);
		order.put("number", orderFormMap.get("number"));
		order.put("shipping_time", orderFormMap.get("shipping_time"));
		order.put("order_state", 2);
		order.put("delete_state", 0);
		order.put("finish_time", null);
		order.put("receiving_date", null);
		order.put("production_date", null);
		order.remove("id");
		try {
			orderMapper.addEntity(order);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return "false";
		}
		return "success";
	}
	
	@RequestMapping("order_CheckToPlanner")
	@ResponseBody
	public String order_CheckToPlanner(String id){
		OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id,
				OrderFormMap.class);
		if(orderFormMap.getInt("order_state")==1){
			return "true";
		}else{
			return "false";
		}
	}
	
	// 新增订单计划页面
	@RequestMapping("add_orderplanner")
	public String add_orderplanner(Model model) throws Exception {
		String id = getPara("id");
		OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id,
				OrderFormMap.class);
		List<ClientPlannerFormMap> orderperFormMaps = clientPlannerMapper
				.findByAttribute("order_id", id, ClientPlannerFormMap.class);
		orderFormMap.put("current",DateUtils.getStringDate());
		model.addAttribute("order", orderFormMap);
		
		model.addAttribute("clientplanner", orderperFormMaps);
		return Common.BACKGROUND_PATH + "/client/clientPlanner_add";
	}
	//订单计划
	@RequestMapping("orderplanner_list")
	public String client_orderplanner(Model model) throws Exception {
		String id = getPara("id");
		OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id,
				OrderFormMap.class);
		List<ClientPlannerFormMap> orderperFormMaps = clientPlannerMapper
				.findByAttribute("order_id", id, ClientPlannerFormMap.class);
		model.addAttribute("order", orderFormMap);
		model.addAttribute("clientplanner", orderperFormMaps);
		return Common.BACKGROUND_PATH + "/client/clientPlanner_list";
	}
	
	@RequestMapping("client_orderPlanneraddEntity")
	@ResponseBody
	public String client_orderPlanneraddEntity(HttpServletRequest request) {
		String[] shipdate=request.getParameterValues("plannerDate");
		String[] shipnum=request.getParameterValues("plannernum");
		OrderFormMap orderFormMap=getFormMap(OrderFormMap.class);
		ClientPlannerFormMap clientPlannerFormMap=getFormMap(ClientPlannerFormMap.class);
		//删除原来保存的
		try {
			clientPlannerMapper.deleteByAttribute("order_id", orderFormMap.get("id").toString(), ClientPlannerFormMap.class);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return "false";
		}
		for (int i = 0; i < shipnum.length; i++) {
			if (i==shipnum.length-1) {
				continue;
			}
			clientPlannerFormMap.put("order_id", orderFormMap.get("id"));
			clientPlannerFormMap.put("material_time", shipdate[i]);
			clientPlannerFormMap.put("number", shipnum[i]);
			clientPlannerFormMap.put("creattime", DateUtils.getStringDate());
			try {
				clientPlannerMapper.addEntity(clientPlannerFormMap);
				clientPlannerFormMap.remove("id");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "false";
			}
		}
		return "success";
	}
}
